2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_uart_ex1_loopback115200Baud.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_A UART - Loopback at 115200 Baud
34  *
35  * Description: In this example, the UART RX and TX pins are connected to
36  * create a loopback of communication data. The example shows how to properly
37  * initialize UART communication at baud rate of 115200 using SMCLK as the
38  * source. After the byte is put into the buffer to send the device goes into
39  * LPM until it has received the sent byte. If data corruption occurs then
40  * the LED will be turned on indicating a problem.
41  *
42  *
43  * MSP430i2041
44  * ------------------
45  * /|\| |
46  * | | P1.4|--->LED
47  * --|RST |
48  * | P1.2/UCA0RXD|----|
49  * | | |
50  * | | |
51  * | P1.3/UCA0TXD|----|
52  * | |
53  *
54  * Author: Zack Lalanne
55  ******************************************************************************/
56 
57 #include "driverlib.h"
58 
59 uint8_t TXData = 1;
60 uint8_t RXData = 0;
61 
62 int main(void)
63 {
64 
65  // Configuration for 115200 UART with SMCLK at 16384000
66  // These values were generated using the online tool available at:
67  // http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
68  EUSCI_A_UART_initParam uartConfig = {
69  EUSCI_A_UART_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
70  8, // BRDIV = 8
71  14, // UCxBRF = 14
72  34, // UCxBRS = 34
73  EUSCI_A_UART_NO_PARITY, // No Parity
74  EUSCI_A_UART_MSB_FIRST, // MSB First
75  EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
76  EUSCI_A_UART_MODE, // UART mode
77  EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION // Oversampling Baudrate
78  };
79 
80  WDT_hold(WDT_BASE);
81 
82  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
83  CS_setupDCO(CS_INTERNAL_RESISTOR);
84 
85  // SMCLK should be same speed as DCO. SMCLK = 16.384MHz
86  CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_1);
87 
88  // Settings P1.2 and P1.3 as UART pins. P1.4 as LED output
89  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
90  GPIO_PIN2 | GPIO_PIN3,
91  GPIO_PRIMARY_MODULE_FUNCTION);
92  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
93  GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
94 
95  // Configure and enable the UART peripheral
96  EUSCI_A_UART_init(EUSCI_A0_BASE, &uartConfig);
97  EUSCI_A_UART_enable(EUSCI_A0_BASE);
98 
99  EUSCI_A_UART_enableInterrupt(EUSCI_A0_BASE,
100  EUSCI_A_UART_RECEIVE_INTERRUPT);
101 
102  while(1)
103  {
104  EUSCI_A_UART_transmitData(EUSCI_A0_BASE, TXData);
105 
106  // Go to sleep and wait for LPM exit
107  __bis_SR_register(LPM0_bits | GIE);
108  }
109 }
110 
111 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
112 #pragma vector=USCI_A0_VECTOR
113 __interrupt
114 #elif defined(__GNUC__)
115 __attribute__((interrupt(USCI_A0_VECTOR)))
116 #endif
117 void USCI_A0_ISR(void)
118 {
119  switch(__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG)) {
120  case USCI_NONE: break;
121  case USCI_UART_UCRXIFG:
122 
123  RXData = EUSCI_A_UART_receiveData(EUSCI_A0_BASE);
124 
125  // Check if data got corrupted
126  if(RXData != TXData) {
127  GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
128  while(1);
129  }
130 
131  TXData++;
132 
133  // Leave LPM so next byte can be sent
134  __bic_SR_register_on_exit(LPM0_bits);
135 
136  break;
137  case USCI_UART_UCTXIFG: break;
138  case USCI_UART_UCSTTIFG: break;
139  case USCI_UART_UCTXCPTIFG: break;
140  default: break;
141  }
142 }
143 
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)